package graph.day08;

import java.util.Scanner;

public class Kama_108 {

    static int[] father;

    public static void union(int x, int y) {
        int fx = find(x);
        int fy = find(y);
        if (fx != fy) {
            father[fx] = fy;
        }
    }

    private static int find(int x) {
        if (father[x] == x) return x;
        else {
            father[x] = find(father[x]);
            return father[x];
        }
    }

    public static boolean isSame(int x, int y) {
        int fx = find(x);
        int fy = find(y);
        return fx == fy;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        father = new int[n + 1];
        for (int i = 1; i < n + 1; i++) {
            father[i] = i;
        }
        for (int i = 0; i < n; i++) {
            int start = sc.nextInt();
            int end = sc.nextInt();
            if (isSame(start, end)) {
                System.out.println(start + " " + end);
                break;
            } else {
                union(start, end);
            }
        }
    }
}
